package com.fyzk.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fyzk.activity.news.NewsListBean;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * @author 指尖沙漏
 * @mood
 * @description
 * @date 2019/8/30 15:37
 * </p>
 */
public class ArticleAdapter extends BaseAdapter {

    private List<NewsListBean.DataBean> dataBeans;
    private Context mContext;

    public ArticleAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<NewsListBean.DataBean> dataBeans) {
        this.dataBeans = dataBeans;
        this.notifyDataSetChanged();
    }

    public List<NewsListBean.DataBean> getData() {
        if (dataBeans == null) {
            dataBeans = new ArrayList<>();
        }
        return dataBeans;
    }

    @Override
    public int getCount() {
        if (dataBeans == null) return 0;
        return dataBeans.size();
    }

    @Override
    public Object getItem(int position) {
        if (dataBeans == null) return null;
        return dataBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_article, parent, false);
            holder = new ViewHolder();
            holder.tvTitle = convertView.findViewById(R.id.articleItem_tvTitle);
            holder.tvTime = convertView.findViewById(R.id.articleItem_tvTime);
            holder.ivArticle = convertView.findViewById(R.id.articleItem_ivArticle);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NewsListBean.DataBean dataBean = dataBeans.get(position);

        holder.tvTitle.setText(dataBean.getTitle());
        if (!TextUtils.isEmpty(dataBean.getIcon_url())) {
            Glide.with(mContext).load(dataBean.getIcon_url()).into(holder.ivArticle);
        } else {
            holder.ivArticle.setImageResource(R.mipmap.ic_launcher);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long time = Long.parseLong(dataBean.getUpdate_time());
        holder.tvTime.setText(format.format(new Date(time * 1000)));

        return convertView;
    }

    class ViewHolder {
        TextView tvTitle;
        TextView tvTime;
        ImageView ivArticle;
    }

}
